SWEP.Category = "Stargate World Wars"
SWEP.PrintName = "Portable Atlantian Healer"
SWEP.Author = "Prefanatic"
SWEP.Contact = "Ctrl + Alt + Del"
SWEP.Purpose = "Heal!"
SWEP.Instructions = "Primary to heal, Secondary to remove Wraith Sucking effects."
SWEP.Base = "weapon_base";		
SWEP.Slot = 1;
SWEP.SlotPos = 3;
SWEP.DrawAmmo	= true;
SWEP.DrawCrosshair = true;
SWEP.ViewModel = "models/weapons/v_models/v_hdevice.mdl";
SWEP.WorldModel = "models/w_hdevice.mdl";
SWEP.Spawnable = true
SWEP.AdminSpawnable = false

SWEP.Primary.ClipSize = 100;
SWEP.Primary.DefaultClip = 100;
SWEP.Primary.Automatic = false;
SWEP.Primary.Ammo	= "fatiuge";

SWEP.Secondary.ClipSize = -1;
SWEP.Secondary.DefaultClip = -1;
SWEP.Secondary.Automatic = false;
SWEP.Secondary.Ammo = "none";


SET = {}
	SET.Fatigue = 2
	SET.NextFatHeal = 0.2
	SET.NextSet = 0
	SET.StyleSpeed = 0
	SET.RenewFat = 0
	SET.Sounds = {}
	SET.HealthStyle = 1
	SET.NextStyle = 0.2
	
	InHeal = false
	Deployed = false

function SWEP:Deploy()

	InHeal = false
	Deployed = true
	Secondary = false
	
end

function SWEP:SecondaryAttack()

	self:PrimaryAttack( true )
	
end

function SWEP:PrimaryAttack( Secondary )

	if !SERVER then return end
	
	if InHeal then self:RemoveHeal() return end
	
	if self:CanTakeFatigue(SET.Fatigue) and self:CheckTrace() then
	
		local function HealThink()
			
			if self.Weapon:Clip1() - SET.Fatigue < 0 then self:RemoveHeal() return false end
			
			local trace = self.Owner:GetEyeTrace()
				ent = trace.Entity
				
			if trace.Hit and ent:IsPlayer() or string.find(ent:GetClass(), "npc") then 
			
				if !InHeal then InHeal = true end
			
				if CurTime() > SET.NextSet then
				
					SET.NextSet = CurTime() + SET.NextStyle
					ent.HP = ent:Health()
					self.HP = self.Owner:Health()
					
					if !Secondary then ent:SetHealth(ent.HP + SET.HealthStyle) else ent:TakeDamage(SET.HealthStyle, self.Owner) end
					
					self:TakeFatigue(SET.Fatigue)
					SET.StyleSpeed = CurTime() + 3
					
					return true
					
				end
				
			else self:RemoveHeal() return false end
			
		end
		
		hook.Add("Think", "GoauldThink", HealThink)
		
	else self:RemoveHeal() return false end
end

function SWEP:RemoveHeal()

	InHeal = false

	hook.Remove("Think", "GoauldThink")
	
end

function SWEP:CheckTrace()

	local trace = self.Owner:GetEyeTrace()
	
	if trace.Hit then
		
		if string.find(trace.Entity:GetClass(), "npc") then return true end
		
		if trace.Entity:IsPlayer() then return true end
		
	else
		
		return false
		
	end
	
end

function SWEP:TakeFatigue( ammount )
	
	if CLIENT then return end
	
	self.Weapon:SetClip1( self.Weapon:Clip1() - ammount )
	
	SET.NextFatHeal = CurTime() + SET.RenewFat
	
end

function SWEP:CanTakeFatigue( ammount )

if CLIENT then return end

	if self.Weapon:Clip1() - ammount <= 0 then
		self:RemoveHeal()
		return false
	else
		return true
	end
	
end

function SWEP:Think()

if CLIENT then return end
	
	if CurTime() > SET.NextFatHeal and self.Weapon:Clip1() < 100 and CurTime() > SET.StyleSpeed then
		
		SET.NextFatHeal = CurTime() + 0.1
		self.Weapon:SetClip1( self.Weapon:Clip1() + 1 )
		
	end

end

colr = 255
	colg = 255
	colb = 255
	cola = 255
	colah = 255

function SWEP:DrawHUD()
 
	local w = ScrW()
	local h = ScrH()
	local wep = LocalPlayer():GetActiveWeapon():Clip1()
	local load = 100 - wep
	local trace = self.Owner:GetEyeTrace()
	if trace.Hit and trace.Entity:IsPlayer() or string.find(trace.Entity:GetClass(), "npc") then pheal = trace.Entity:Health() else pheal = 0 end
	
	if load >= 0 and load < 30 then colr = math.Approach(colr, 255, 5) colg = math.Approach(colg, 255, 5) colb = math.Approach(colb, 255, 5) end
	if load >= 30 and load < 50 then colr = math.Approach(colr, 50, 5) colg = math.Approach(colg, 50, 5) colb = math.Approach(colb, 255, 5) end
	if load >= 50 and load < 70 then colr = math.Approach(colr, 50,  5) colg = math.Approach(colg, 255, 5) colb = math.Approach(colb, 50, 5) end
	if load >= 70 and load < 90 then colr = math.Approach(colr, 255, 5) colg = math.Approach(colg, 50, 5) colb = math.Approach(colb, 255, 5) end
	if load >= 90 and load <= 100 then colr = math.Approach(colr, 255, 5) colg = math.Approach(colg, 50, 5) colb = math.Approach(colb, 50, 5) end
	if load >= 10 and load <= 100 then cola = math.Approach(cola, 255, 5) else cola = math.Approach(cola, 0, 5) end
	if trace.Hit and trace.Entity:IsPlayer() or string.find(trace.Entity:GetClass(), "npc") then colah = math.Approach(colah, 255, 5) else colah = math.Approach(colah, 0, 5) end
	local col = Color(colr, colg, colb, cola)
	
	draw.DrawText( "Weapon Load: " .. load .. "%", "Trebuchet22", w / 2, h / 2, col, 1)
	if trace.Hit and trace.Entity:IsPlayer() or string.find(trace.Entity:GetClass(), "npc") then draw.DrawText( "Health: " .. pheal .. "%", "Trebuchet22", w / 2, h / 2 + 30, Color( 255, 255, 255, colah ), 1 ) end
	
end
--if CLIENT and Deployed then hook.Add("HUDPaint", "GoauldHUD", DrawFatigue) end